#include <iostream>  
#include <memory>  
  
using namespace std;  
  
struct ListNode {  
    int val;  
    std::unique_ptr<ListNode> next;  
    ListNode(int x) : val(x), next(nullptr) {}  
};  
  
void insert(ListNode* &head, int val) {  
    std::unique_ptr<ListNode> node = std::make_unique<ListNode>(val);  
    node->next = std::move(head);  
    head = std::move(node);  
}  
  
void deleteNode(ListNode* &head, int val) {  
    if (head == nullptr) return;  
    if (head->val == val) {  
        head = std::move(head->next);  
        return;  
    }  
    ListNode* prev = head;  
    ListNode* cur = head->next;  
    while (cur != nullptr && cur->val != val) {  
        prev = cur;  
        cur = cur->next;  
    }  
    if (cur != nullptr) {  
        prev->next = std::move(cur->next);  
    }  
}  
  
void traverse(ListNode* head) {  
    ListNode* cur = head;  
    while (cur != nullptr) {  
        cout << cur->val << " ";  
        cur = cur->next.get();  
    }  
    cout << endl;  
}  
  
int main() {  
    ListNode* head = nullptr;  
  
    insert(head, 1);  
    insert(head, 2);  
    insert(head, 3);  
    insert(head, 4);  
    insert(head, 5);  
  
    traverse(head);  
  
    deleteNode(head, 3);  
  
    traverse(head);  
  
    return 0;  
}  
